from PIL import ImageGrab


class BarResources:
    def __init__(self, gameWindow, config: dict):
        self.region_x = gameWindow.x + config['region_x']
        self.region_y = gameWindow.y + config['region_y']
        self.region_w = config['region_w']
        self.color_false = tuple(config['color_false'])

    def have_resource_of_num(self, num):

        try:
            x = self.region_x + self.region_w * (num - 1)
            color = ImageGrab.grab(bbox=(x, self.region_y, x + 1, self.region_y + 1)).getpixel(
                (0, 0))
            # 获取该像素的颜色
            if color != self.color_false:
                return True
            else:
                return False
        except Exception as e:
            print(f"获取像素颜色失败: {e}")
            return False